package com.example.yuejian21.nongfusanquan.yuejian21.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.yuejian21.nongfusanquan.yuejian21.R;
import com.example.yuejian21.nongfusanquan.yuejian21.activity.CouchdetailActivity;
import com.example.yuejian21.nongfusanquan.yuejian21.javabean.Couchbean;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by Nongfusanquan on 2017-10-12.
 */



public class CouchAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private Context mContex;
    private List mDataList;

    private LayoutInflater mLayoutInflater;

    public CouchAdapter(Context mContext, List mDataList) {
        this.mContex =  mContext;
        this.mDataList = mDataList;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = mLayoutInflater.from(parent.getContext()).
                inflate(R.layout.cardview_couch, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
         final Couchbean entity = (Couchbean) mDataList.get(position);
        if (null == entity)
            return;
        CouchAdapter.ViewHolder viewHolder = (CouchAdapter.ViewHolder) holder;
        viewHolder.t_name.setText(entity.getPm_name());
        viewHolder.couchinfo.setText(entity.getPm_info());

        final String t_picture = "http://10.0.2.2/yuejian14/Public/Admin/couchphotos/"+entity.getPm_pic();
        if (!t_picture.equals("")) {
            Picasso.with(mContex).load(t_picture).into(viewHolder.t_picture);
        }
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle=new Bundle();
                bundle.putInt("pm_id",entity.getPm_id());
                bundle.putString("pm_name",entity.getPm_name());
                bundle.putString("pm_info",entity.getPm_info());
                bundle.putString("pm_pic",t_picture);
                Intent intent=new Intent(mContex,CouchdetailActivity.class);
                intent.putExtras(bundle);
                mContex.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView t_picture;
        TextView t_name,couchinfo;

        public ViewHolder(View itemView) {
            super(itemView);
            t_name = (TextView) itemView.findViewById(R.id.couch_name);
            t_picture = (ImageView) itemView.findViewById(R.id.couch_pic);
            couchinfo = (TextView)itemView.findViewById(R.id.couch_info);
        }
    }
}


